<template>
  <div class="back">
    <div
      style=" display: flex; align-items: center; justify-content: center; background-color: rgba(255,255,255,.8); width: 28%; border-radius: 10px;">
      <!-- 表单验证 :rules="rules" ||  ref="loginRef" 方便拿到这个表单 -->
      <el-form :model="user" :rules="rules" ref="loginRef">
        <div style="font-size: 20px;font-weight: bold; text-align: center; margin-bottom: 20px; cursor: default;">
          矿区航拍影像地裂缝智能识别系统</div>
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号：" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码："
            v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item prop="yzm">
          <div style="width:100%;display: flex;align-items: center;">
            <el-input prefix-icon="el-icon-lock" size="medium" placeholder="请输入验证码：" v-model="user.yzm"></el-input>
            <img style="height: 30px;margin-left:20px;cursor: pointer" src="../assets/yzm.png" alt="">
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%;" @click="login">登 录</el-button>
        </el-form-item>
        <div style="cursor: default; display: flex;">
          <div style="flex: 1;">还没有账号？请 <span style="color: forestgreen; cursor: pointer; font-weight: bold;" @click="$router.push('/register')">注册</span></div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "LoginView",
  data() {
    return {
      user: {
        username: 'admin',
        password: 'admin123'
      },
      rules: {
        // 必须跟上面输入账号的 prop="username" 一样，而且必须在自定的  user 对象里有 username
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      }
    }
  },
  created() {

  },
  methods: {
    login() {
      // 上面在el-form定义的ref在这里用
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          this.$request.post('user/login', this.user).then(res => {
            if (res.code === 200) {
              this.$router.push('/')

              this.$message.success('登录成功')

              localStorage.setItem("honey-user", JSON.stringify(res.data))

              setTimeout(() => {
                this.$router.push('/'); // 使用 setTimeout 延迟跳转
              }, 100);

            } else {
              // 返回我定义的错误信息 如：账号或密码错误
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style>
.back {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('../assets/background.jpg');
  background-size: cover;

}

.el-form {
  margin: 15px;
  width: 80%;

}
</style>